package de.tum.in.eist.poll.server;

import de.tum.in.eist.poll.shared.Feedback;
import de.tum.in.eist.poll.shared.Lecture;
import de.tum.in.eist.poll.shared.Lecturer;
import de.tum.in.eist.poll.shared.Student;
import de.tum.in.eist.poll.shared.User;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public final class Database
{
  private static List<Lecture> lectures;
  private static List<User> users;
  private static List<Student> students;
  private static List<Lecturer> lecturers;
  private static Map<Lecture, List<Feedback>> lectureToFeedback = new LinkedHashMap<Lecture, List<Feedback>>();

  public static void addFeedbackToLecture(Lecture lecture, Feedback feedback)
  {
    List<Feedback> feedbackForLecture = (List<Feedback>)lectureToFeedback.get(lecture);

    if (feedbackForLecture == null)
    {
      feedbackForLecture = new LinkedList<Feedback>();
      feedbackForLecture.add(feedback);
      lectureToFeedback.put(lecture, feedbackForLecture);
    }
    else {
      feedbackForLecture.add(feedback);
    }
  }

  public static boolean isValidInput(String name, String password)
  {
    User user = getUser(name);
    if (user != null) {
      return user.getPassword().equals(password);
    }
    return false;
  }

  public static User getUser(String name)
  {
    if (users == null) {
      users = new LinkedList<User>();

      users.addAll(getStudents());
      users.addAll(getLecturers());
    }

    for (User user : users) {
      if (user.getName().equals(name)) {
        return user;
      }
    }

    return null;
  }

  public static List<Student> getStudents()
  {
    if (students == null) {
      students = new LinkedList<Student>();

      students.add(new Student("Student_1", "new"));
      students.add(new Student("Student_2", "new"));
    }
    return students;
  }

  public static List<Lecturer> getLecturers()
  {
    if (lecturers == null) {
      lecturers = new ArrayList<Lecturer>(2);

      lecturers.add(new Lecturer("Prof", "new"));
      lecturers.add(new Lecturer("Maalej", "new"));
    }
    return lecturers;
  }

  public static List<Lecture> getLectures()
  {
    if (lectures == null) {
      lectures = new ArrayList<Lecture>(3);

      Lecturer lecturer = (Lecturer)getLecturers().get(0);
      lectures.add(new Lecture("IN0001", "EIST_1", lecturer, false));
      lectures.add(new Lecture("IN0002", "EIST_2", lecturer, false));

      lecturer = (Lecturer)getLecturers().get(1);
      lectures.add(new Lecture("IN0003", "EIST_3", lecturer, false));
    }
    return lectures;
  }

  public static Lecture getLecture(String lectureID)
  {
    for (Lecture lecture : lectures) {
      if (lecture.getLectureID().equals(lectureID)) {
        return lecture;
      }
    }

    return null;
  }

  public static List<Feedback> getFeedback(Lecture lecture)
  {
    return (List<Feedback>)lectureToFeedback.get(lecture);
  }
}
